//
//  FnumViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/20.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "FnumViewController.h"
#import "LBAFNetworking.h"
#import "LuBDefine.h"
#import "FnumTableViewCell.h"
#import "DetailOfPhotographViewController.h"

@interface FnumViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *fnumArray;

@end

@implementation FnumViewController

- (void)dealloc{
    [_pid release];
    [_fnumArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.fnumArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self downloadData];
    [self createFnumViewController];
    // Do any additional setup after loading the view.
}

- (void)downloadData{
    NSString *url = [NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/photo.html?page=1&do=zanlist&pid=%@&limit=10&develop=0",self.pid];
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        NSMutableArray *tempArray = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in tempArray) {
            Fnums *fnums = [Fnums fnumWithDictionary:dic];
            [self.fnumArray addObject:fnums];
        }
        self.tableView.separatorStyle =1;
        [self.tableView reloadData];
    
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createFnumViewController{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"伙伴";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CWIDTH, CHEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.fnumArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FnumTableViewCell *fnumCell = [FnumTableViewCell fnumtableViewWithTableView:tableView];
    fnumCell.fnums = self.fnumArray[indexPath.row];
    return fnumCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 10 + CWIDTH / 8 + 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailOfPhotographViewController *detailOfPhotographViewController = [[DetailOfPhotographViewController alloc]init];
    NSString *uid = [self.fnumArray[indexPath.row] uid];
    NSString *petname = [self.fnumArray[indexPath.row] petname];
    detailOfPhotographViewController.uid = uid;
    detailOfPhotographViewController.petname = petname;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailOfPhotographViewController animated:YES];
    [detailOfPhotographViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//页面边缘手势导航栏变不透明,tableView布局发生变化
- (void)viewDidAppear:(BOOL)animated{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
